<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
require_once ( LIB_DIR.'classes/Pager.php' );
require_once ( LIB_DIR.'search/reviewsearch.php' );

global $logid,$uid,$user;

$errmsg = GetMessageStack();
$pageVars = array();

$reviewdao = GetReviewDAO();

$reviews = array();
$reviewCount = 0;

$pageVars['uid'] = $uid;

$page = intval( GET('p') );
if($page<=0)
{
	$page=1;
}

$filterArray = array();
$searchType = DB_QUERY;
    
$sortby = "review_date";
if( isset( $_GET['sortby'] ) ) {
	
	$sortby = htmlspecialchars( edp_db_input( $_GET['sortby'] ) );
	
	$sortby =  $sortby == "" ? "review_date" : $sortby;
	
    $pageVars['sortby'] = $sortby;
}

    //other three parameters
$catid = "";
if( isset( $_GET['catid'] )&&!edp_is_empty_string($_GET['catid']) ) {
	$catid = intval( $_GET['catid'] );
        $pageVars['catid'] = $catid;

	//for filter search
	$searchType = FILTER_QUERY;
	$filterArray['cate_id_list'] = array( intval($catid) );

	$categoryDao = GetCatgoryDAO();
	$pageVars['catname'] = $categoryDao->GetShopCateNameByCid( $catid );
}

$tag = "";
if( isset( $_GET['tag'] )&&!edp_is_empty_string($_GET['tag']) ) {
	$tag = edp_db_input( $_GET['tag'] );
        $pageVars['tag'] = $tag;

	$searchType = NORMAL_QUERY;
}

/*
$startdate = "";
$enddate = "";
if(isset( $_GET['start_date'] ) && isset( $_GET['end_date'] ) ){
	$startdate = strtotime(edp_db_input( $_GET['start_date'] ));
	$enddate = strtotime(edp_db_input( $_GET['end_date'] ));

	//do date validation
	if( $startdate<$enddate ){
		$strDateRange = $startdate."," .$enddate;
		//add field specified query to $keyword parameter
		$filterArray['review_date'] = $strDateRange;
		$searchType = FILTER_QUERY;
	}
}
*/


//$currentURL = $_SERVER["REQUEST_URI"];
//$currentURL = preg_replace( '/&p=[0-9]+/', "", $currentURL );
$currentURL=edp_href_link(PAGENAME_USER_REVIEW, 'uid='.$uid.'&sortby='.$sortby.'&catid='.$catid.'&tag='.$tag);
	
//different type queries
$result = array();
switch ($searchType) {
	case DB_QUERY:
		//normal database query
		$reviewCount = $reviewdao->GetReviewCountByUserId( $uid );
		$reviews = $reviewdao->GetReviewByUid( $uid, REVIEW_PER_PAGE, $page, $sortby );
		$pageVars['reviewCount'] = $reviewCount;
		if( $reviews ) {
			
			// to get pics and products for this review
			// added by yawei
			$reviews = $reviewdao->GetReviewDetail($reviews);
			
			$pageVars['reviews'] = array();
			foreach( $reviews as $review )
				$pageVars['reviews'][] = $review;
				
			// prepare page bar of search result
			$pageObj = new Pager($currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );
			$pageVars['pagebar'] = $pageObj->getBar();
		}
		break;
	case NORMAL_QUERY:
		$reviewSearch = new ReviewSearch();
		$result = $reviewSearch->GetReviewByTag( $tag, $page, $uid );
		//$result = $reviewSearch->Query( $tag, $page );
		break;
	case FILTER_QUERY:
		$reviewSearch = new ReviewSearch();
		$result = $reviewSearch->MultiFilterQuery( "", $filterArray, $page, $uid );
		break;
	default:
		break;
}

//assemble search result
if( !empty($result ) ){
	if( intval($result['total']) > 0 && !empty($result['matchIds']) ){
		$reviews = $reviewdao->GetByIdList( $result[ 'matchIds'] );
		$pageVars['reviews'] = $reviews;
		$reviewCount = $result['total'];

		// prepare page bar of search result
		$pageObj = new Pager($currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );
		$pageVars['pagebar'] = $pageObj->getBar();
	}
}

?>
